package com.dxf.sysadmin.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.dxf.common.utils.ResultJson;
import com.dxf.sysadmin.entity.EvaluateBo;
import com.dxf.sysadmin.entity.EvaluateTypeBo;
import com.dxf.sysadmin.mapper.AddEvaluateTypeMapper;
import com.dxf.sysadmin.utils.ExcelListener;
import com.dxf.sysadmin.utils.ExcelStListener;
import com.dxf.sysadmin.utils.ExcelTeListener;
import com.dxf.sysadmin.utils.ExcelTypeListener;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/appraise")
public class ExcelController {
    @Resource
    AddEvaluateTypeMapper addEvaluateTypeMapper;

    @PostMapping("import")
    public ResultJson batchImport(@RequestParam("file") MultipartFile file) throws IOException {
        addEvaluateTypeMapper.DeleEveryIt();
        addEvaluateTypeMapper.DeleEveryTa();
        addEvaluateTypeMapper.DeleEveryTy();
        ExcelReader excelReader = EasyExcel.read(file.getInputStream()).build();
        ReadSheet readSheet1 = EasyExcel.readSheet(0).head(EvaluateTypeBo.class).registerReadListener(new ExcelTypeListener()).build();
        ReadSheet readSheet2 = EasyExcel.readSheet(1).head(EvaluateBo.class).registerReadListener(new ExcelListener()).build();
        ReadSheet readSheet3 = EasyExcel.readSheet(2).head(EvaluateBo.class).registerReadListener(new ExcelTeListener()).build();
        ReadSheet readSheet4 = EasyExcel.readSheet(3).head(EvaluateBo.class).registerReadListener(new ExcelStListener()).build();
        excelReader.read(readSheet1, readSheet2, readSheet3, readSheet4);
        // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
        excelReader.finish();
        return ResultJson.ok().code("0");
    }
}
